require("../css/common.css")
require("../css/mine.less")
document.ready(function(){
    //调用登录拦截
    window.$utils.checkLogin()
    //调用公共的footer
    window.$utils.footer(3)
    //修改头像
    const BASE_URL = 'http://139.9.177.51:8099' // 接口请求的 URL
    let avatar=""
    //用户id
    let user=JSON.parse(localStorage.getItem("user")).userId

    let userImg=document.querySelector("#userImg")
    let imgUploadIpt=document.querySelector("#imgUploadIpt")
    userImg.addEventListener("click",function(event){
        event.preventDefault()
        imgUploadIpt.click()
    })
    imgUploadIpt.addEventListener("change",function(){
        // console.dir(this);
        window.$updateFile("/users/upload","imgurl",this.files[0],function(res){
            console.log(res);
            if(res.status===0){
                avatar=BASE_URL+res.data
                userImg.src=avatar
                //成功上传之后立即修改账号信息
                window.$http.post("/users/userEdit",{
                    imgurl:avatar,
                    userId:user
                },function(res){
                    console.log(res);
                    if(res.status===0){
                        window.$utils.toast(res.status,res.msg,"头像上传",function(){

                            location.reload()
                        })
                    }
                })
            }
        })
    })

    //页面加载获取用户信息
    let nickName=document.querySelector("#nickName")
    let userSign=document.querySelector("#userSign")
    window.$http.get("/users/accountinfo?userId="+user,function(res){
        console.log(res);
        userImg.src=res.data.imgurl
        nickName.textContent=res.data.nickname
        userSign.textContent=res.data.sign
    })
    //退出登录
    let logout=document.querySelector("#logout")
    logout.addEventListener("click",function(){
        window.localStorage.removeItem("token")
        window.localStorage.removeItem("user")
        location.href="../login.html"
    })

})